package com.crm.service.saling.impl;

import com.crm.dao.saling.RefundDao;
import com.crm.exception.ServiceException;
import com.crm.pojo.Inandoutlog;
import com.crm.pojo.Refund;
import com.crm.service.saling.RefundService;
import com.crm.vo.RefundContractClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import java.util.List;

/**
 * Created by dr5hx on 2018/8/24
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RefundServiceImpl implements RefundService {
    @Autowired
    private RefundDao refundDao;

    @Override
    public List<RefundContractClient> getRefundContractClientList(int page, int rows) throws ServiceException {
        try {
            return refundDao.getRefundContractClientList(new PageRequest(page - 1, rows));
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public Long getRefundCount() {
        return refundDao.getRefundCount();
    }

    /**
     * 保存一个退款单
     *
     * @param refund
     * @return
     */
    @Override
    public Refund saveARefund(Refund refund) throws ServiceException {
        try {
            return refundDao.save(refund);
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServiceException(e);

        }
    }

    @Override
    public List<Inandoutlog> findAllBymh() {
        List<Inandoutlog> list = new ArrayList<>();
        List<Refund> refund = refundDao.findAllBymh();
        for (Refund r:refund) {
            Inandoutlog io = new Inandoutlog();
            io.setIoDispatherId(r.getRefundId());
            io.setIoDispatherTitle(r.getRefundTopic());
            list.add(io);
        }
        return list;
    }
    @Override
    public Refund findOneById(Long rid){
        return refundDao.findOne(rid);
    }
}
